package com.jg.test.Page;

import com.jg.test.AnyTest;
import com.jg.test.Dice;
import com.jg.test.Monster.Monster;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

// 造成伤害
public class Battle004 extends Page {
    @Override
    public void effect() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        // 标记此次使用的骰子
        for (int i = 0; i < 6; i++) {
            Dice.dice.get(i).setUsed(Battle002.dice.get(i).isUsed());
        }
        // 伤害结算
        Class<?> clazz = AnyTest.team.get(4).getClass();
        Method method = clazz.getMethod("skill" + (Battle001.choice));
        int[] dmg = (int[]) method.invoke(AnyTest.team.get(4));
        // 逐滴分配伤害，保证伤害不溢出
        // 伤害类型，[x,y]:x伤害量，y伤害类型，0可分担，1不可分担，2群体伤害
        int dmgPoint = 0;
        int choice;
        int hp;
        while (!(dmgPoint == dmg[0])) {
            switch (dmg[1]) {
                case 0:
                    monsterShow();
                    System.out.println("你将造成1点伤害，请分配");
                    choice = Page.numGet(AnyTest.monsters.get(0).size() - 1);
                    hp = AnyTest.monsters.get(0).get(choice).getHealth();
                    AnyTest.monsters.get(0).get(choice).setHealth(hp - 1);
                    aliveCheck();
                    monsterShow();
                    break;
                case 1:
                    monsterShow();
                    System.out.println("你将造成"+ dmg[0] + "点伤害，请选择目标");
                    choice = Page.numGet(AnyTest.monsters.get(0).size() - 1);
                    hp = AnyTest.monsters.get(0).get(choice).getHealth();
                    AnyTest.monsters.get(0).get(choice).setHealth(hp - dmg[0]);
                    dmgPoint = dmg[0];
                    aliveCheck();
                    monsterShow();
                    break;
                case 2:
                    monsterShow();
                    System.out.println("你将对每个怪物造成造成"+ dmg[0] + "点伤害");
                    for (Monster m:AnyTest.monsters.get(0)
                         ) {
                        hp = m.getHealth();
                        m.setHealth(hp - dmg[0]);
                        aliveCheck();
                    }
                    dmgPoint = dmg[0];
                    monsterShow();
                    break;
            }
        }
    }

    @Override
    public void sendMessage() {
        System.out.println("0.继续使用技能\n1.结束回合");
    }

    @Override
    public void next() {
        if (AnyTest.monsters.get(0).size() == 0){
            AnyTest.monsters.remove(0);
            AnyTest.pageNavigation.add(new Start001());
        }
        AnyTest.pageNavigation.add(new Affected000());
    }

    private void monsterShow(){
        int i = 0;
        for (Monster m:AnyTest.monsters.get(0)
             ) {
            System.out.println(i + "." + m.getName() + "  " + m.getHealth() + "/" + m.getHealthLimit());
            i++;
        }
    }
    private void aliveCheck(){
        for (Monster m:AnyTest.monsters.get(0)
             ) {
            if (m.getHealth() <= 0){
                AnyTest.monsters.remove(m);
            }
        }
    }
}
